package com.example.springboot.service.ServiceImpl;

import com.example.springboot.common.Page;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能
 * 作者：程序员小白
 * 日期：2024-09-22 10:38
 */
@Service
public class UseServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }
    @Override
    public void deleteUser(int id) {
        userMapper.deleteUser(id);
    }
    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }
    @Override
    public void BatchdeleteUser(List<Integer> ids) {
        for(int id : ids){
            userMapper.deleteUser(id);
        }
    }
    @Override
    public List<User> selectAllUser(){
        List<User> users = userMapper.selectAllUser();
        for (User a : users) {
            System.out.println(a);
        }
        return users;
    }
    @Override
    public User selectUserById(Integer id) {
        User user = userMapper.selectUserById(id);
        return user;
    }

    @Override
    public List<User> selectUserByName(String name) {
        List<User> user = userMapper.selectUserByName(name);
        for (User users : user){
            System.out.println(users.toString());
        }
        return user;
    }

    @Override
    public List<User> selectByMo(String username,String name) {
        List<User> user = userMapper.selectByMo(username,name);
        for (User users : user){
            System.out.println(users.toString());
        }
        return user;
    }

    @Override
    public Page<User> selectByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Integer skipNum = (pageNum - 1) * pageSize;  // 计算出来  1 -> 0,5    2 -> 5,5   3 -> 10,5
        Page<User> page = new Page<>();
        List<User> userList = userMapper.selectByPage(skipNum, pageSize, username, name);
        Integer total = userMapper.selectCountByPage(username, name);
        page.setTotal(total);
        page.setList(userList);
        return page;
    }

    @Override
    public User login(User user) {
        User dbuser = userMapper.selectByUserName(user.getUsername());
        if(dbuser == null){
            throw new ServiceException("账号或密码错误");
        }
        if(!dbuser.getPassword().equals(user.getPassword())){
            throw new ServiceException("账号或密码错误");
        }
        return dbuser;
    }

    @Override
    public User register(User user) {
        User dbuser = userMapper.selectByUserName(user.getUsername());
        if(dbuser != null){
            throw new ServiceException("用户名已存在");
        }
        user.setName(user.getUsername());
        userMapper.insertUser(user);
        return user;
    }
}
